﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

using ProjectBase.Core;

namespace ProjectBase.Mvc
{
    public enum MethodType
    {
        Post,
        Get,
    }

    /// <summary>
    /// The &lt;FORM&gt; html element. Also renders the AntiForgeryToken,
    /// unless the RemoveAntiForgeryToken() is called
    /// </summary>
    public class Form : ContentControl<ICoreModel, IVisualControl, Form>
    {
        #region members
        #endregion members

        #region constructor
        /// <summary>
        /// The FORM html element constructor.
        /// Unless the RemoveAntiForgeryToken() is called, the AntiForgery token is also appended
        /// </summary>
        /// <param name="method"></param>
        /// <param name="asMultipart"></param>
        public Form(MethodType method = MethodType.Post, bool? asMultipart = null)
        {
            if (asMultipart.HasValue
                && asMultipart.Value)
            {
                EncType = "multipart/form-data";
            }
            Method = method.ToString().ToLower();
        }
        #endregion constructor

        protected override void RenderEndTag(System.Web.UI.HtmlTextWriter writer)
        {
            if (TagName.IsEmpty())
            {
                return;
            }
            if (!GenerateAntiForgeryToken.HasValue && Method.Equals(MethodTypes.Post)
              || GenerateAntiForgeryToken.HasValue && GenerateAntiForgeryToken.Value)
            {
                writer.Write(System.Web.Helpers.AntiForgery.GetHtml());
            }
            base.RenderEndTag(writer);
        }

        #region Set
        public virtual Form SetEncType(string type)
        {
            EncType = type;
            return this;
        }

        public virtual Form SetMethod(MethodType method)
        {
            Method = method.ToString().ToLower();
            return this;
        }

        public virtual Form SetName(string name)
        {
            Name = name;
            return this;
        }

        public override Form SetId(string id)
        {
            FormId = id;
            return this;
        }

        public virtual Form SetAction(string action)
        {
            Action = action;
            return this;
        }
        public virtual Form RemoveAntiForgeryToken()
        {
            GenerateAntiForgeryToken = false;
            return this;
        }
        #endregion Set

        #region properties
        protected override string TagName
        {
            get { return Action.IsNotEmpty() ? Tag.Form : string.Empty; }
        }

        public string EncType
        {
            get { return GetAttribute(Attr.EncType); }
            set { SetAttribute(Attr.EncType, value); }
        }

        public string Method
        {
            get { return GetAttribute(Attr.Method); }
            set { SetAttribute(Attr.Method, value); }
        }

        public virtual string Name
        {
            get { return GetAttribute(Attr.Name); }
            set { SetAttribute(Attr.Name, value); }
        }

        public virtual string FormId
        {
            get { return GetAttribute(Attr.ID); }
            set { SetAttribute(Attr.ID, value); }
        }

        public virtual string Action
        {
            get { return GetAttribute(Attr.Action); }
            set { SetAttribute(Attr.Action, value); }
        }

        public virtual bool? GenerateAntiForgeryToken { get; set; }
        #endregion properties
    }
}